package br.com.mmm.javacenter.framework.reflection.injectors;

import java.lang.reflect.Field;

import org.apache.log4j.Logger;

import br.com.mmm.javacenter.framework.annotations.InjectorComponent;
import br.com.mmm.javacenter.framework.dao.HibernateUtil;
import br.com.mmm.javacenter.framework.dao.RepositoryBase;
import br.com.mmm.javacenter.framework.entity.EntidadeBase;
import br.com.mmm.javacenter.framework.entity.TipoStatus;
import br.com.mmm.javacenter.framework.reflection.ReflectionFacade;

@InjectorComponent(EntidadeBase.class)
public class EntidadeBaseInjector implements Injector{

	private static Logger logger = Logger.getLogger(EntidadeBaseInjector.class);
	private RepositoryBase repository;
	
	public void inject(ReflectionFacade rf, String field, Object value) {
		Field f = rf.getField(field);
		EntidadeBase entidadeBase = null;
		
		if(EntidadeBase.class.isAssignableFrom(f.getType())){
		 	try {
		 		entidadeBase = (EntidadeBase) f.getType().newInstance();
				if(value.toString().contains("-")){
					String[] split = value.toString().split("-");
					entidadeBase.setId(Long.parseLong(split[0]));
				}else{
					entidadeBase.setId(Long.parseLong(value.toString()));
				}
			} catch (Exception e) {
				logger.debug(e.getMessage());
			}
		}
		rf.set(field, repository.consultaPorCodigo(entidadeBase));
	}

	public RepositoryBase getRepository() {
		return repository;
	}

	public void setRepository(RepositoryBase repository) {
		this.repository = repository;
	}
	
}
